package com.chinasofti.bpm.activiti;

import org.activiti.engine.*;
import org.activiti.engine.repository.Deployment;
import org.activiti.engine.repository.DeploymentBuilder;
import org.activiti.engine.repository.ProcessDefinition;
import org.activiti.engine.repository.ProcessDefinitionQuery;
import org.activiti.engine.runtime.ProcessInstance;
import org.activiti.engine.task.Task;
import org.junit.Test;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * activiti的常见功能
 */
public class ActivitiCommonFunc_sequenceProcess {

    //创建activiti的业务表
    @Test
    public void creatTable(){
         try{
             ProcessEngine processEngine=ProcessEngineConfiguration.createProcessEngineConfigurationFromResource("activiti.cfg.xml").buildProcessEngine() ;
             System.out.println(processEngine) ;
         }catch(Exception e){
             e.printStackTrace();
         }
         }
    //部署流程
    @Test
    public void deployProcess(){
        ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine();
        RepositoryService repositoryService = processEngine.getRepositoryService();
        DeploymentBuilder builder = repositoryService.createDeployment();
        builder.addClasspathResource("squenceProcess.bpmn");//bpmn文件的名称
        Deployment deployment= builder.deploy();
        System.out.println(deployment.getId()+"      "+deployment.getName()) ;
    }
    //启动流程
    @Test
    public void startProcess(){
        ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine();
        RuntimeService runtimeService = processEngine.getRuntimeService();
        ProcessInstance pi= runtimeService.startProcessInstanceByKey("squenceProcess");//流程的名称，也可以使用ByID来启动流程
        System.out.println(pi.getId()+"    "+pi.getActivityId()) ;
    }
    //查询任务
    @Test
    public void queryTask(){
        ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine();
        TaskService taskService = processEngine.getTaskService();
        //根据assignee(代理人)查询任务
        String assignee = "张一";
        List<Task> tasks = taskService.createTaskQuery().taskAssignee(assignee).list();

        int size = tasks.size();
        for (int i = 0; i < size; i++) {
            Task task = tasks.get(i);

        }
        //首次运行的时候这个没有输出，因为第一次运行的时候扫描act_ru_task的表里面是空的，但第一次运行完成之后里面会添加一条记录，之后每次运行里面都会添加一条记录
        for (Task task : tasks) {
            System.out.println("taskId:" + task.getId() +
                    ",taskName:" + task.getName() +
                    ",assignee:" + task.getAssignee() +
                    ",createTime:" + task.getCreateTime());
        }

        // 流程定义查询对象，用于查询流程定义表（act_re_procdef）
        ProcessDefinitionQuery query = processEngine.getRepositoryService().createProcessDefinitionQuery();
        // 以下查询的是所有的流程定义
        List<ProcessDefinition> list = query.list();
        for (ProcessDefinition pd : list) {
            System.out.println(pd.getId() + "    " + pd.getName() + "    " + pd.getVersion());
        }
        }

        //完成任务
    @Test
    public  void completeTask() throws Exception{
        ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine();
        Map<String ,Object> variables=new HashMap<String,Object>() ;
        variables.put("message","重要") ;
        processEngine.getTaskService().complete("30003"); ;
        System.out.println("完成任务") ;
    }

    //完成任务,带条件
    @Test
    public  void completeTaskWithConditon() throws Exception{
        ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine();
        Map<String ,Object> variables=new HashMap<String,Object>() ;
        variables.put("message","不重要") ;
        processEngine.getTaskService().complete("37504",variables); ;
        System.out.println("完成任务") ;
    }

}
